<?php

namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Common\Model\Power;
use Common\Model\Admin;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        'App\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        //获取所有权限列表
        $allPower = Power::where(['status' => 1, 'type' => 2])->get(['id', 'alias', 'status', 'type']);
        $admin = new Admin;

        foreach ($allPower as $power) {
            Gate::define($power->alias, function () use ($admin, $power) {
                return $admin->hasRole($power->alias);
            });
        }
    }

}
